package com.ydgk.dao;

import com.ydgk.bean.Order;
import com.ydgk.bean.Product;
import com.ydgk.bean.User;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDao extends BaseDao<Product> {
    @Override
    public int save(Product product) {
        return this.template.update("insert into y_product values(null,?,?,?,?,?,?)",product.getImage(),product.getProductName(),product.getDiscribe()
        ,product.getPrice(),product.getBrand(),product.getType());
    }
    @Override
    public int updateById(Product product) {
        return this.template.update("update y_product set image=?,productName=?,discribe=?,price=?,brand=?,type=? where id=?",
                product.getImage(),product.getProductName(),product.getDiscribe(),product.getPrice(),product.getBrand(),product.getType(),product.getId());
    }

    @Override
    public int deleteById(int id) {
        return template.update("delete from y_product where id=?",id);
    }

    @Override
    public List<Product> findAll() {
        return this.template.query("select * from y_product",new BeanPropertyRowMapper<Product>(Product.class));
    }

    @Override
    public Product findById(int id) {
        return null;
    }

    @Override
    public List<Product> paging(int pageNumber) {
        return null;
    }
    public List<Product> adminSearch(String productName, Integer pageNumber) {
        StringBuilder sql=new StringBuilder("select p.id as pid,p.image,p.productName,p.discribe,p.price,p.brand,p.type from y_product p");
        List params=new ArrayList();
        if(productName!=null&&!productName.equals("")){
            sql.append(" where p.productName=?");
            params.add(productName);
        }
        sql.append(" order by pid asc limit ?,? ");
        params.add((pageNumber - 1) * pageCount);
        params.add(pageCount);
        return  this.template.query(sql.toString(), new ResultSetExtractor<List<Product>>() {
            @Nullable
            @Override
            public List<Product> extractData(ResultSet rs) throws SQLException, DataAccessException {
                List<Product> productList=new ArrayList<Product>();
                while (rs.next()){
                    Product product=new Product();
                    product.setId(rs.getInt("pid"));
                    product.setImage(rs.getString("image"));
                    product.setProductName(rs.getString("productName"));
                    product.setDiscribe(rs.getString("discribe"));
                    product.setPrice(rs.getInt("price"));
                    product.setBrand(rs.getString("brand"));
                    product.setType(rs.getString("type"));
                    productList.add(product);
                }
                return productList;
            }
        },params.toArray());
    }
    public int adminSearchMaxPage(String productName) {
        StringBuilder sql = new StringBuilder("select count(*) from y_product");
        List params = new ArrayList();
        if (productName != null && !productName.equals("")) {
            sql.append(" where productName=?");
            params.add(productName);
        }
        long t = this.template.queryForObject(sql.toString(), Long.class, params.toArray());

        int total = (int) t;//将long类型转换成int类型
        return total % pageCount == 0 ? total / pageCount : total / pageCount + 1;
    }
}
